package com.bincky.common;

import org.apache.commons.io.FileUtils;
import org.apache.log4j.Logger;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;

import java.io.File;
import java.io.IOException;
import java.util.Set;

/**
 * 描述:
 *
 *
 * @date 2021-01-26 22:53:46
 */

public class BaseCase {

    public static WebDriver driver;
    public static String browser;

    //初始化日志对象
    public Logger logger = Logger.getLogger(BaseCase.class);

    /**
     * 选择不同浏览器打开
     */
    public void openBrowser(String browserName){
        if (browserName.equals("chrome")){
            browser="chrome";
            System.setProperty("webdriver.chrome.driver", "src/test/resources/chromedriver.exe");
            /*//headless-->无头/无页面显示
            //1.ChromeOptions对象
            ChromeOptions chromeOptions = new ChromeOptions();
            //2.添加配置
            chromeOptions.addArguments("--headless");
            driver = new ChromeDriver(chromeOptions);*/
            driver = new ChromeDriver();
            logger.info("===================打开Chrome浏览器===================");
        }else if (browserName.equals("firefox")){
            browser="firefox";
            System.setProperty("webdriver.firefox.bin", "D:\\\\Software\\\\Mozilla Firefox\\\\firefox.exe");
            System.setProperty("webdriver.gecko.driver", "src/test/resources/geckodriver.exe");
            driver = new FirefoxDriver();
            logger.info("===================打开Firefox浏览器===================");
        }
    }

    /**
     * 打开网页
     */
    public void openUrl(String url){
        logger.info("打开网页【"+url+"】");
        driver.get(url);
    }

    /**
     * 关闭浏览器
     */
    public void closeBrowser(){
        logger.info("===================测试完毕，关闭驱动===================");
        driver.quit();
    }

    /**
     * 浏览器最大化
     */
    public void maximizeBrowser(){
        logger.info("浏览器最大化");
        driver.manage().window().maximize();
    }

    /**
     * 刷新浏览器
     */
    public void refreshBrowser(){
        logger.info("刷新浏览器");
        WebDriver.Navigation navigation = driver.navigate();
        navigation.refresh();
    }

    /**
     * 浏览器前进
     */
    public void browserForward(){
        logger.info("浏览器前进");
        driver.navigate().forward();
    }

    /**
     * 浏览器后退
     */
    public void browserBackward(){
        logger.info("浏览器后退");
        driver.navigate().back();
    }

    /**
     * 获取当前页面的URL
     */
    public String getCurrentUrl(){
        logger.info("获取页面当前URL");
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return driver.getCurrentUrl();
    }

    /**
     * 根据窗口句柄，判断页面title切换至对应窗口
     */
    public void switchWindow(String title){
        logger.info("切换至title为【"+title+"】的窗口");
        Set<String> handles = driver.getWindowHandles();
        for (String handle :
                handles) {
            driver.switchTo().window(handle);
            if (driver.getTitle().equals(title)){
                break;
            }
        }
    }

    /**
     * 断言二次封装
     * 判断表达式是否为真
     * @param condition
     */
    public void myAssertTrue(boolean condition){
        logger.info("表达式【"+condition+"】是否为真");
        Assert.assertTrue(condition);
    }

    /**
     * 断言二次封装
     * 判断实际值与期望值是否一致
     * @param actualValue
     * @param expectedValue
     */
    public void myAssertEquals(String actualValue,String expectedValue){
        logger.info("判断实际值【"+actualValue+"】和期望值【"+expectedValue+"】是否一致");
        Assert.assertEquals(actualValue, expectedValue);
    }

    /**
     * 执行截图操作
     * @param
     */
    public static File takeScreenShot(){
        File srcFile = null;
        //根据浏览器的名字去判断
        if (browser.equals("chrome")){
            //webDriver类型的driver对象强制转换成ChromeDriver,因为ChromeDriver有getScreenshotAs方法
            ChromeDriver chromeDriver = (ChromeDriver) driver;
            //截图返回的是File类型
            srcFile = chromeDriver.getScreenshotAs(OutputType.FILE);
        }else if (browser.equals("firefox")){
            FirefoxDriver firefoxDriver = (FirefoxDriver) driver;
            srcFile = firefoxDriver.getScreenshotAs(OutputType.FILE);
        }
        //file对象转换成图片
        File destFile = new File("target/ScreenShot/screenshot_"+System.currentTimeMillis()+".png");
        try {
            //copy操作
            FileUtils.copyFile(srcFile, destFile);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return destFile;
    }


}
